package com.sptpc.demo1.controller;

import com.sptpc.demo1.model.Drink;
import com.sptpc.demo1.model.Student;
import com.sptpc.demo1.service.IStudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@Slf4j  //日志
@RestController
@RequestMapping("/stu")
public class StudentController {

    @Autowired //在合适的地方自动实例化对象；
    private IStudentService studentService;

    @GetMapping("/all")
    public List<Student> getall(){
        List<Student> students=studentService.getAllDrinks();
        return students;
    }

    @PostMapping("/add")
    public Student addNewStudent(@RequestBody Student student){
        log.info("新增: {}",student);
        // 后期使用数据库insert
        studentService.addNewStudent(student);
        return student;
    }
    @DeleteMapping("/del/{id}")
    public String delete(@PathVariable Long id){
        log.info("要删除的id: {}",id);
        //后期使用数据库
        studentService.delStudent(id);
        return "删除操作成功";
    }

    @GetMapping("/{id}")
    public Student get(@PathVariable Long id){
        LocalDate localDate = LocalDate.of(2019, 9, 1);
        log.info("查询的id为: {}",id);
        Student student=(Student.builder()
                .id(1L)
                .major("移动应用开发")
                .name("张三")
                .adminssjonDate(localDate)
                .build());
        return student;
    }
    @PutMapping("/update")
    public Student put(@RequestBody Student request){
        log.info("要修改的饮品: {}",request);
        LocalDate localDate = LocalDate.of(2019, 9, 1);
        // 后期使用数据库update
        //1. 先通过id查询出对应的数据信息
        Student student=studentService.getStudentById(request.getId());
        //2. 复制前端要修改的属性(将前端传递的值覆盖个drink)
        BeanUtils.copyProperties(request,student);
        //3. 更新数据库
        studentService.updateDrink(student);
        return student;
    }
    /**
     * 批量上传文档
     * @return
     */
    @PostMapping(value = "/batch",consumes ={MediaType.MULTIPART_FORM_DATA_VALUE})
    public List batchAddNewDrink(MultipartFile file){
        List<Student> list=new ArrayList<>();
        try {
            InputStreamReader inputStreamReader=new InputStreamReader(file.getInputStream());
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line="";
            while ((line=bufferedReader.readLine())!=null){
                String [] args=line.split(",");
                String date1 = args[3];
                DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd");
                LocalDate date2 = LocalDate.parse(date1, fmt);
                Student student= Student.builder()
                        .id(Long.parseLong(args[0]))
                        .name(args[1])
                        .major(args[2])
                        .adminssjonDate(date2).build();
                list.add(student);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return list;
    }
}
